<?php
/**
 * 各种排行榜
 */
namespace app\smartbc\controller\api\v1;
use app\common\model\SystemUser;
use app\smartbc\controller\api\Base;
use app\smartbc\model\SmartbcAdwords;
use think\helper\Time;

class Ranking extends Base{


    //省钱榜冠军
    public function topfirst(){
        $rel = $this->apiSign();
        if ($rel['code'] != 200) {
            return enjson($rel['code'],'签名验证失败');
        }
        list($start, $end) = Time::lastMonth();
        $condition[] = ['u.update_time','>=',$start];
        $condition[] = ['u.update_time','<=',$end];
        $list = SystemUser::alias('c')->where(['c.member_miniapp_id' => $this->miniapp_id])->join('SmartbcCouponUser u', 'c.id = u.uid')->where(['u.member_miniapp_id' => $this->miniapp_id,'is_end' => 1])->where('money','<>',null)->where($condition)
            ->field('c.face,c.nickname,sum(money) as save')->group('u.uid')->order('money desc,c.create_time desc')->find();
        if (empty($list)) {
            return enjson(204,'没有数据');
        }
        $list->month = date('n',$start);
        return enjson(200,$list);
    }
    
    //省钱榜
    public function topMoney(){
        $param['page'] = $this->request->param('page/d',1);
        $rel = $this->apiSign($param);
        if ($rel['code'] != 200) {
            return enjson($rel['code'],'签名验证失败');
        }
        $list = SystemUser::alias('c')->where(['c.member_miniapp_id' => $this->miniapp_id])->join('SmartbcCouponUser u', 'c.id = u.uid')->where(['u.member_miniapp_id' => $this->miniapp_id,'is_end' => 1])->where('money','<>',null)
            ->field('c.face,c.nickname,sum(money) as save')->group('u.uid')->order('money desc,c.create_time desc')->page($param['page'],10)->select();
        if ($list->isEmpty()) {
            return enjson(204,'没有数据');
        }
        return enjson(200,$list);
    }
}